﻿<System.ComponentModel.DefaultEvent("ColorChanged")>
 Public Class ScheduleTypeColorView

    Event ColorChanged(sender As Object, e As ColorChangedEventArgs)
    Private _scheduleColors As ScheduleColorSetting
    Public Property ScheduleColors() As ScheduleColorSetting
        Get
            Return _scheduleColors
        End Get
        Set(ByVal value As ScheduleColorSetting)
            _scheduleColors = value
            LoadColors()
        End Set
    End Property
    Private _scheduleTypes As List(Of String)
    Public Property ScheduleTypes() As List(Of String)
        Get
            Return _scheduleTypes
        End Get
        Set(ByVal value As List(Of String))
            _scheduleTypes = value
            LoadColors()
        End Set
    End Property


    Private Function GetCompColor(BackGround As Color) As Color
        Return Color.FromArgb(BackGround.ToArgb Xor &HFFFFFF)
    End Function
    Private Sub LoadColors()

        For Each g As DevExpress.XtraEditors.TileGroup In tlColors.Groups
            g.Items.Clear()

        Next

        If Me.ScheduleColors IsNot Nothing AndAlso Me.ScheduleTypes IsNot Nothing Then
            For Each t In Me.ScheduleTypes.Except(Me._scheduleColors.Keys)
                Me.ScheduleColors.Add(t, New XmlColor(Color.White))
            Next
            For Each cs In Me.ScheduleColors

                Dim grp As DevExpress.XtraEditors.TileGroup
                If Me.ScheduleTypes.Contains(cs.Key) Then
                    grp = grpUsed
                Else
                    grp = grpUnUsed
                End If

                Dim tl As New DevExpress.XtraEditors.TileItem()
                With tl
                    .Text = cs.Key
                    .Tag = cs.Key
                    Dim compColor As Color = GetCompColor(cs.Value.ToColor)
                    compColor = Color.FromArgb(255, compColor)
                    With .AppearanceItem.Normal
                        .BackColor = cs.Value.ToColor()
                        .ForeColor = compColor
                    End With
                    With .AppearanceItem.Selected
                        .BackColor = cs.Value.ToColor()
                        .ForeColor = compColor
                    End With
                End With
                tl.TextAlignment = DevExpress.XtraEditors.TileItemContentAlignment.MiddleCenter

                grp.Items.Add(tl)
            Next




            grpUnUsed.Visible = grpUnUsed.Items.Count > 0



        End If

    End Sub


    Private LoadingColor As Boolean

    Private Sub tlColors_SelectedItemChanged(sender As System.Object, e As DevExpress.XtraEditors.TileItemEventArgs) Handles tlColors.SelectedItemChanged
        For Each itm In (From g As DevExpress.XtraEditors.TileGroup In tlColors.Groups From i As DevExpress.XtraEditors.TileItem In g.Items Where i.Checked = True Select i).ToList
            itm.Checked = False

        Next

        ColorPickEdit1.Enabled = tlColors.SelectedItem IsNot Nothing
        btnDelete.Enabled = tlColors.SelectedItem IsNot Nothing AndAlso grpUnUsed.Items.Contains(tlColors.SelectedItem)


        If tlColors.SelectedItem Is Nothing Then Return
        tlColors.SelectedItem.Checked = True
        LoadingColor = True
        ColorPickEdit1.Color = Me.ScheduleColors(tlColors.SelectedItem.Tag)
        LoadingColor = False

    End Sub

    Private Sub ColorPickEdit1_EditValueChanged(sender As System.Object, e As System.EventArgs) Handles ColorPickEdit1.EditValueChanged
        If LoadingColor Then Return
        Me.ScheduleColors(tlColors.SelectedItem.Tag) = New XmlColor(ColorPickEdit1.Color)
        With tlColors.SelectedItem
            Dim compColor As Color = GetCompColor(ColorPickEdit1.Color)
            With .AppearanceItem.Normal
                .BackColor = ColorPickEdit1.Color
                .ForeColor = compColor
            End With
            With .AppearanceItem.Selected
                .ForeColor = compColor
                .BackColor = ColorPickEdit1.Color
            End With
        End With
        RaiseEvent ColorChanged(Me, New ColorChangedEventArgs(tlColors.SelectedItem.Tag, ColorPickEdit1.Color))
    End Sub

    Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
        If grpUnUsed.Items.Contains(tlColors.SelectedItem) Then
            Me.ScheduleColors.Remove(tlColors.SelectedItem.Tag)
            tlColors.SelectedItem = Nothing
            LoadColors()

        End If
    End Sub
    Private Loaded As Boolean
    Private Sub ScheduleTypeColorView_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Loaded = True
    End Sub

   
    Class ColorChangedEventArgs
        Inherits EventArgs
        Property newColor As Color
        Property ScheduleType As String
        Public Sub New(ScheduleType As String, newColor As Color)
            Me.newColor = newColor
            Me.ScheduleType = ScheduleType

        End Sub
    End Class

End Class
